<template>
  <div>
    <section>
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.keyword" placeholder="分类模板名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="queryTemp">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleEdit()">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="temp" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>

        <el-table-column prop="id" label="模板id" width="160" sortable>
        </el-table-column>

        <el-table-column prop="name" label="分类模板名称" width="160" sortable>
        </el-table-column>

        <el-table-column prop="specIds" label="关联品牌" width="160" sortable>
          <template scope="scope">
            <span v-for="specid in JSON.parse(scope.row.specIds)">{{specid.text}}&nbsp</span>
          </template>
        </el-table-column>

        <el-table-column prop="brandIds" label="关联规格" width="320" sortable>
          <template scope="scope">
            <span v-for="brandId in JSON.parse(scope.row.brandIds)">{{brandId.text}}&nbsp</span>
          </template>
        </el-table-column>

        <el-table-column prop="customAttributeItems" label="扩展属性" width="160" sortable>
          <template scope="scope">
            <span v-for="customAttributeItem in JSON.parse(scope.row.customAttributeItems)">{{customAttributeItem.text}}&nbsp</span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" :disabled="this.sels.length===0" @click="batchRemove">批量删除</el-button>
        <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
        </el-pagination>
      </el-col>

<!--     &lt;!&ndash;        &lt;!&ndash;编辑界面&ndash;&gt;&ndash;&gt;-->
      <el-dialog title="编辑" :visible.sync="editTempVisible" :close-on-click-modal="false">
        <el-form :model="editTemp" label-width="80px" :rules="editTempRules" ref="editTemp">


          <el-form-item label="模板名称" prop="name">
            <el-input v-model="editTemp.name" auto-complete="off"></el-input>
          </el-form-item>

          <el-form-item label="关联品牌" prop="specIds">
            <el-select v-model="editTemp.specIds" multiple placeholder="请选择">
              <el-option
                  v-for="item in SpecList"
                  :key="item.id"
                  :label="item.specName"
                  :value="item.id+'|'+item.specName">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="关联规格" prop="brandIds">
            <el-select v-model="editTemp.brandIds" multiple placeholder="请选择">
              <el-option
                  v-for="item in BrandList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id+'|'+item.name">
              </el-option>
            </el-select>
          </el-form-item>

          <el-button type="primary" plain @click="addeditTemp">新增扩展属性</el-button>

          <el-table :data="editTemp.customAttributeItems" style="width: 100%;">
            <el-table-column prop="customAttributeItems" label="属性名称" width="160" sortable>
              <template scope="scope">
                <el-input size="small" v-model="scope.row.text" type="text"></el-input>
              </template>
            </el-table-column>

            <el-table-column label="操作">
              <template scope="scope">
                <el-button type="danger" size="small" @click="customAttributeItemsDel(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>



        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editTempVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
        </div>
      </el-dialog>

    </section>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name: "TempView",
  data(){
    return{
      filters: {
        keyword: ''
      },
      temp:[],
      listLoading:false,
      sels:[],
      total: 0,
      pageNum: 1,
      pageSize:3,
      editLoading:false,
      editTempVisible:false,
      SpecList:[],
      BrandList:[],
      editTemp:{
        id:null,
        name: null,
        customAttributeItems:[{},{},{}],
        specIds: [],
        brandIds:[],
      },
      editTempRules: {
        name: [
          { required: true, message: '请输入模板名称', trigger: 'blur' }
        ]
      },
    }
  },
  methods:{
    // 查询
    queryTemp(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum,   //页码
        name:this.filters.keyword,
      }
      this.$http.post("/system/api/temp/queryPage",params)
          .then(resp=>{
            this.temp = resp.data.data.list;
            this.total = resp.data.data.total;
          }).catch(resp=>{
        this.$message({
          message: "请登录",
          type: 'error'
        });
      })
    },


    selsChange: function (sels) {
      this.sels = sels;
    },

    addeditTemp:function () {
      this.editTemp.customAttributeItems.push({})
    },

    handleCurrentChange(val) {
      this.pageNum = val;
      this.queryTemp();
    },

    customAttributeItemsDel:function (index) {
      this.editTemp.customAttributeItems.splice(index,1)
    },

    //编辑
    editSubmit: function () {
      this.$refs.editTemp.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;

            let specJson =[];
            this.editTemp.specIds.map(item=>{
                  let arr=item.split("|");
                  let obj={
                    id:arr[0],
                    text:arr[1]
                  }
                  specJson.push(obj)
            })
            this.editTemp.specIds=JSON.stringify(specJson)


            let brandJson =[];
            this.editTemp.brandIds.map(item=>{
              let arr=item.split("|");
              let obj={
                id:arr[0],
                text:arr[1]
              }
              brandJson.push(obj)
            })
            this.editTemp.brandIds=JSON.stringify(brandJson)

            let customArr =[];
            this.editTemp.customAttributeItems.map(item=>{
              let obj={
                text:item.text
              }
              customArr.push(obj)
            })
            this.editTemp.customAttributeItems=JSON.stringify(customArr)

            let para = Object.assign({}, this.editTemp);

            this.$http.put("/system/api/temp/save",para)
                .then(resp=>{
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.editLoading=false;
                    this.editTempVisible=false;
                    this.queryTemp();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                    this.editLoading=false;
                    this.editTempVisible=false;
                  }
                }).catch(resp=>{
              this.editLoading=false;
              this.$message({
                message: "网络异常，稍后重试",
                type: 'error'
              });
            })
          });
        }
      });
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/system/api/temp/delete/"+row.id;
        this.$http.delete(url)
            .then(resp=>{
                  //去除忙等框
                  this.listLoading = false;
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.queryTemp();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                  }
                }
            )
            .catch(result=>{
              //去除忙等框
              this.listLoading = false;
              //提示
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/system/api/temp/batchRemove",ids)
            .then(resp=>{
                  //去除忙等框
                  this.listLoading = false;
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.queryTemp();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                    this.listLoading = false;
                  }
                }
            )
      }).catch(() => {
        this.listLoading = false;
        //提示
        this.$message({
          message: '系统错误！',
          type: 'error'
        });
      })
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.editTemp={
            name: null,
        customAttributeItems:[{},{},{}],
            specIds: [],
            brandIds:[],
      },
      this.querySpecList();
      this.queryBrandList();
      if(row!=null){
        this.editTemp.name=row.name;

        this.editTemp.id=row.id;

        let SpecArr =JSON.parse(row.specIds);
        SpecArr.forEach(e=>{
          this.editTemp.specIds.push(e.id+'|'+e.text);
        })

        let BrandArr =JSON.parse(row.brandIds);
        BrandArr.forEach(e=>{
          this.editTemp.brandIds.push(e.id+'|'+e.text);
        })

        let customArr =JSON.parse(row.customAttributeItems)
        this.editTemp.customAttributeItems=Object.assign([],customArr)

      }
      this.editTempVisible = true;
    },

    querySpecList: function () {
      let params = {
        pageSize: 999, //页面大小
        pageNum: 1,   //页码
      }
      this.$http.post("/system/api/spec/queryPage",params)
          .then(resp=>{
            this.SpecList = resp.data.data.list;
          }).catch(resp=>{
        this.$message({
          message: "网络异常，稍后重试",
          type: 'error'
        });
      })
    },

    queryBrandList: function () {
      let params = {
        pageSize: 999, //页面大小
        pageNum: 1,   //页码
      }
      this.$http.post("/system/api/brand/queryPage",params)
          .then(resp=>{
            this.BrandList = resp.data.data.list;
          }).catch(resp=>{
        this.$message({
          message: "网络异常，稍后重试",
          type: 'error'
        });
      })
    },

  },
  mounted() {
    this.queryTemp();
    this.querySpecList();
    this.queryBrandList();
  }
}
</script>

<style scoped>

</style>